using System;
using UnityEngine;

namespace com.adjust.sdk.purchase
{
	public class AdjustPurchase : MonoBehaviour
	{
		private const string errorMessage = "AdjustPurchase: SDK not started. Start it manually using the 'start' method.";

		private static IAdjustPurchase instance;

		private static Action<ADJPVerificationInfo> verificationInfoCallback;

		public bool startManually = true;

		public string appToken = "{Your App Token}";

		public ADJPLogLevel logLevel = ADJPLogLevel.Info;

		public ADJPEnvironment environment;

		private void Awake()
		{
			if (instance == null)
			{
				UnityEngine.Object.DontDestroyOnLoad(base.transform.gameObject);
				if (!startManually)
				{
					ADJPConfig aDJPConfig = new ADJPConfig(appToken, environment);
					aDJPConfig.SetLogLevel(logLevel);
					Init(aDJPConfig);
				}
			}
		}

		public static void Init(ADJPConfig config)
		{
			if (instance != null)
			{
				Debug.Log("AdjustPurchase: Error, purchase SDK already started.");
				return;
			}
			if (config == null)
			{
				Debug.Log("AdjustPurchase: Missing config to start.");
				return;
			}
			instance = null;
			if (instance == null)
			{
				Debug.Log("AdjustPurchase: Purchase SDK can only be used in Android and iOS apps.");
			}
			else
			{
				instance.Init(config);
			}
		}

		public static void VerifyPurchaseiOS(string receipt, string transactionId, string productId, Action<ADJPVerificationInfo> verificationInfoCallback, string sceneName = "AdjustPurchase")
		{
			if (instance == null)
			{
				Debug.Log("AdjustPurchase: SDK not started. Start it manually using the 'start' method.");
				return;
			}
			if (receipt == null || transactionId == null || verificationInfoCallback == null)
			{
				Debug.Log("AdjustPurchase: Invalid purchase parameters.");
				return;
			}
			AdjustPurchase.verificationInfoCallback = verificationInfoCallback;
			instance.VerifyPurchaseiOS(receipt, transactionId, productId, sceneName);
		}

		public static void VerifyPurchaseAndroid(string itemSku, string itemToken, string developerPayload, Action<ADJPVerificationInfo> verificationInfoCallback, string sceneName = "AdjustPurchase")
		{
			if (instance == null)
			{
				Debug.Log("AdjustPurchase: SDK not started. Start it manually using the 'start' method.");
				return;
			}
			if (itemSku == null || itemToken == null || developerPayload == null || verificationInfoCallback == null)
			{
				Debug.Log("AdjustPurchase: Invalid purchase parameters.");
				return;
			}
			AdjustPurchase.verificationInfoCallback = verificationInfoCallback;
			instance.VerifyPurchaseAndroid(itemSku, itemToken, developerPayload, verificationInfoCallback);
		}

		public void GetNativeVerificationInfo(string stringVerificationInfo)
		{
			if (instance == null)
			{
				Debug.Log("AdjustPurchase: SDK not started. Start it manually using the 'start' method.");
				return;
			}
			if (verificationInfoCallback == null)
			{
				Debug.Log("AdjustPurchase: Attribution changed delegate was not set.");
				return;
			}
			ADJPVerificationInfo obj = new ADJPVerificationInfo(stringVerificationInfo);
			verificationInfoCallback(obj);
		}
	}
}
